package demo4.mapper;

import demo4.Util.IDBUtil;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Mapper是数据持久层的类,作用是和数据库进行操作,提供基础的增删改查
 */
@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;
    public int insert(User user){
        System.out.println("UserMapper:插入用户数据");
        //连数据库
        try (Connection connection = dbUtil.getConnection()){
            String sql = "INSERT INTO user(username,password,nickname,age) "+
                         "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();

        } catch (Exception e) {
          e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User selectByUsername(String username) {
        /*

        * */
        try (Connection connection = dbUtil.getConnection()){
          String sql = "SELECT id,username,password,nickname,age "+
                        "FROM user " +
                        "WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(User user) {
        try (  Connection connection = dbUtil.getConnection()){
            String sql = "update user " +
                    "set password = ?,nickname=?,age=? " +
                    "where username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(4, user.getUsername());
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getNickname());
            ps.setInt(3,user.getAge());
          //  int num = ps.executeUpdate();
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;

    }

    @Override
    public List<User> selectAll() {
        List<User> list = new ArrayList<>();
        try(  Connection connection = dbUtil.getConnection()) {

          String sql = "select id,username,password,nickname,age from user";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String nickname = resultSet.getString("nickname");
                int age = resultSet.getInt("age");
                User user = new User();
                user.setId(id);
                user.setUsername(username);
                user.setPassword(password);
                user.setNickname(nickname);
                user.setAge(age);

                list.add(user);
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public int deleteByUsername(String username) {
        int num = 0 ;
        try(Connection connection = dbUtil.getConnection()) {
            String sql = "DELETE FROM user " +
                    "WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
             num = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

//    @Override
//    public boolean selectUser(String username, String password) {
//
//        try(Connection connection = dbUtil.getConnection()){
//            String sql = "select id,username,password,nickname,age " +
//                    "from user " +
//                    "where username=? and password =?";
//            PreparedStatement ps = connection.prepareStatement(sql);
//            ps.setString(1,username);
//            ps.setString(2,password);
//            ResultSet rs = ps.executeQuery();
//            if(rs.next()){
//                String usernameOld = rs.getString("username");
//                String passwordOld = rs.getString("password");
//             if(username.equals(usernameOld)&&password.equals(passwordOld)){
//                 return true;
//             }else {
//                 return false;
//             }
//
//            }else{
//                return false;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }


}
